/**
 * Copyright 2011-2013 - Reliable Bits Software by Blommers IT. All Rights Reserved.
 * Author Rick Blommers
 */

#include <QApplication>
#include <QDebug>
#include <QList>
#include <QTimer>

#include "../vendor/qslog/QsLog.h"
#include "../vendor/qslog/QsLogDest.h"

#include "edbee/util/test.h"
#include "edbee/edbee.h"

#include "edbee/debug.h"



#include <QApplication>
#include <QDebug>
#include <QFile>
#include <QMainWindow>

#include "edbee/edbee.h"
#include "edbee/models/textdocument.h"
#include "edbee/models/textgrammar.h"
#include "edbee/texteditorwidget.h"
#include "edbee/views/textrenderer.h"
#include "edbee/views/texttheme.h"


/// This is an example that shows how to load a grammar file and a theme file
/// manually and initialise the editor with it
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    // initialize edbee
    edbee::Edbee* edbee = edbee::Edbee::instance();
    edbee->autoInit();

    // read the grammar file
    edbee::TextGrammarManager* grammarManager = edbee->grammarManager();
    edbee::TextGrammar* grammar = grammarManager->readGrammarFile( "HTML.tmLanguage" );
   // edbee::TextGrammar* grammar = grammarManager->readGrammarFile( "linux.json" );
    if( !grammar ) {
        qDebug() << "Grammar couldn't be loaded: " << grammarManager->lastErrorMessage();
        edbee->shutdown(); // call this method manuall to prevent memory leak warnings. When the exec method is called this isn't required
        return 1;
    }

    // read the theme file
    edbee::TextThemeManager* themeManager = edbee->themeManager();
    edbee::TextTheme* theme = themeManager->readThemeFile( "SpaceCadet.tmTheme" );
    if( !theme) {
        qDebug() << "Theme couldn't be loaded: " << themeManager->lastErrorMessage();
        edbee->shutdown(); // call this method manuall to prevent memory leak warnings. When the exec method is called this isn't required
        return 1;
    }

    // next create the main window and the editor
    QMainWindow win;
    edbee::TextEditorWidget editor;
    editor.textDocument()->setLanguageGrammar( grammar );
    editor.textRenderer()->setTheme( theme );

#if 1
    editor.textDocument()->setText("<html>\n\t<body>\n\t\t<h1>Hello World</h1>\n\t</body>\n</html>\n");
#else
    // set some initial text
    QFile my("shell.txt");
    if(my.open(QFile::ReadOnly)){
        QString buf = QString::fromUtf8(my.readAll());
        editor.textDocument()->setText(buf);
    }else{
        QString buf;
        buf.append("-rw-rw-rw- 1 root root  4373092 Sep 28 20:29 1111.txt\r\n");
        buf.append("-rw------- 1 root root   987734 Dec 29 07:34 智能船舶规范_2023.pdf\r\n");
        buf.append("[root@VM-12-2-centos ~]# ls -a -c\r\n");
        editor.textDocument()->setText(buf);
    }
#endif
    // show the window
    win.setCentralWidget( &editor );
    win.show();
    return a.exec();
}

/// the main entry method of the test
int main2(int argc, char* argv[])
{
    QApplication app( argc, argv);

    // add all tests that need to be run
    //==================================
    QList<QString> tests;

    tests.append( "edbee::TextChangeTest");
    tests.append( "edbee::MergableChangeGroupTest");
    tests.append( "edbee::LineOffsetVectorTest");
    tests.append( "edbee::LineDataListChangeTest");
    tests.append( "edbee::PlainTextDocumentTest");
    tests.append( "edbee::TextLineDataTest");
    tests.append( "edbee::TextRangeSetTest");
    tests.append( "edbee::TextUndoStackTest");

    tests.clear();      // when the tests lists is empty all tests are run

    // test initialization
    //=====================

    // make sure we see the QsLogging items
    QsLogging::Logger& logger = QsLogging::Logger::instance();
    static QsLogging::DestinationPtrU debugDestination( QsLogging::DestinationFactory::MakeDebugOutputDestination() );
    logger.addDestination(std::move(debugDestination));
    logger.setLoggingLevel(QsLogging::TraceLevel);


    // Load the grammars
    QString appDataPath;
    #ifdef Q_OS_MAC
        appDataPath = app.applicationDirPath() + "/../Resources/";
    #else
        appDataPath= app.applicationDirPath() + "/data/";
    #endif

    // configure the edbee component to use the default paths
    edbee::Edbee* tm = edbee::Edbee::instance();
    //tm->setKeyMapPath( QStringLiteral("%1%2").arg(appDataPath).arg("keymaps"));
    tm->setGrammarPath(  QStringLiteral("%1%2").arg(appDataPath).arg("syntaxfiles") );
    //tm->setThemePath( QStringLiteral("%1%2").arg(appDataPath).arg("themes") );
    tm->init();

    // next run all tests
    if( tests.isEmpty() ) {
        edbee::test::engine().runAll();

    // only run the selected tests
    } else {
        edbee::test::engine().startRun();
        foreach( QString test, tests ) {
            edbee::test::engine().run(test);
        }
        edbee::test::engine().endRun();
    }


    // shutdown edbee
    tm->shutdown();

    return 0;
}

